﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLogic.Poker;

namespace GameLogic.Statistics
{
    public class PlayerStatisticsCounter : IPlayerStatisticsCounter
    {
        public Statistics CountStatistics(IEnumerable<BattlesHistory> userBattles)
        {
            var battles = userBattles.ToList();

            Statistics result = new Statistics();
            CountPreflopStat(battles, result);
            CountAgression(battles, result);
            return result;
        }

        private void CountPreflopStat(List<BattlesHistory> userBattles, Statistics statistics)
        {
            foreach (var bet in userBattles.SelectMany
                (battles => battles.ListBet.Where
                                (bet => bet.Street == GameState.Preflop)))
            {
                statistics.VPIP++;
                if (bet.BetName == BetName.Raise)
                    statistics.PFR++;
            }
            statistics.VPIP = 100 * statistics.VPIP / userBattles.Count;
            statistics.PFR = 100 * statistics.PFR / userBattles.Count;
        }

        private void CountAgression(List<BattlesHistory> userBattles, Statistics statistics)
        {
            var raiseNumber = 0;
            var callNumber = 0;
            var betNumber = 0;

            userBattles.ForEach(battle =>
            {
                battle.ListBet.ForEach(bet =>
                {
                    if (bet.BetName == BetName.Call)
                        callNumber++;
                    if (bet.BetName == BetName.Raise)
                        raiseNumber++;
                });
                betNumber += battle.ListBet.Count;
            });
            statistics.AF = (raiseNumber + betNumber) / callNumber;
        }


    }
}
